<?php
/**
 * DbSssion Functions
 *
 * @author @@AUTHOR@@
 * @copyright @@AUTHOR@@ @@YEAR@@
 * @version @@VERSION@@
 * @package Extensions
 * @subpackage DbSession
 * @license  @@LICENCE_URL@@ @@LICENCE@@
 *
 * $Id: function.php 322 2008-10-11 13:03:00Z dinoboff $
 * $HeadURL: http://vanilla-friends.googlecode.com/svn/trunk/DbSession/library/function.php $
 */

/**
 *  Add DbSession settings to conf/settings.php.
 *
 * @param Context $Context
 * @param string $VersionRef
 */
function DbSession_Setup(&$Context, $VersionRef) {

	if (!array_key_exists('DbSession_VERSION', $Context->Configuration)
		|| version_compare($Context->Configuration['DBSESSION_VERSION'], '0.2.0', '<')
	) {
		AddConfigurationSetting($Context,
			'OBJECT_FACTORY_REFERENCE_USER_MANAGER', 'DbSessionUserManager');
		AddConfigurationSetting($Context,
			'OBJECT_FACTORY_REFERENCE_ROLE_MANAGER', 'DbSessionRoleManager');
		AddConfigurationSetting($Context,
			'OBJECT_FACTORY_REFERENCE_PEOPLE_SESSION', 'DbSessionSession');
		AddConfigurationSetting($Context,
			'AUTHENTICATION_CLASS', 'DbSessionAuthenticator');
		AddConfigurationSetting($Context,
			'LIBRARY_INCLUDE_PATH', '%LIBRARY%;%EXTENSIONS%/DbSession/library/');
	}

	if (!array_key_exists('DBSESSION_VERSION', $Context->Configuration)
		|| version_compare($Context->Configuration['DBSESSION_VERSION'], $VersionRef, '<')
	) {
		// Add configuration settings
		//$DbSessionDir = str_replace('\\', '/', $DbSessionDir);
		$Config = array(
			'DBSESSION_ZEND_FRAMEWORK_PATH_TO_INCLUDE' => '',
			'DBSESSION_ZEND_DB_ADAPTER' => 'Pdo_Mysql',
			'DBSESSION_LAST_UPDATE' => mktime(),
		);
		foreach ($Config as $key => $value) {
			if (!array_key_exists($key, $Context->Configuration)) {
				AddConfigurationSetting($Context, $key, $value);
			}
			$Context->Configuration[$key] = $value;
		}


		AddConfigurationSetting($Context, 'DBSESSION_VERSION', $VersionRef);
	}

	DbSession_InstallDB($Context);
}

/**
 * Add the tables and field require for DbSession
 *
 * @param Context $Context
 */
function DbSession_InstallDB(&$Context) {
	if (! array_key_exists('DbSession_Session', $Context->DatabaseTables)) {
		$TableName = $Context->Configuration['DATABASE_TABLE_PREFIX'] . 'DbSession_Session';
		AppendToConfigurationFile($Context->Configuration['DATABASE_PATH'],
			"\$Configuration['DATABASE_SESSION_HOST'] = '".$Context->Configuration['DATABASE_HOST']."';\n" .
				"\$Configuration['DATABASE_SESSION_NAME'] = '".$Context->Configuration['DATABASE_NAME']."';\n" .
				"\$Configuration['DATABASE_SESSION_USER'] = '".$Context->Configuration['DATABASE_USER']."';\n" .
				"\$Configuration['DATABASE_SESSION_PASSWORD'] = '".$Context->Configuration['DATABASE_PASSWORD']."';\n" .
				"\$Configuration['DATABASE_SESSION_CHARACTER_ENCODING'] = '".$Context->Configuration['DATABASE_CHARACTER_ENCODING']."';\n" .
				"\$DatabaseTables['DbSession_Session'] = '".$TableName."';\n" .
				"\$DatabaseColumns['DbSession_Session']['SessionID'] = 'SessionID';\n" .
				"\$DatabaseColumns['DbSession_Session']['Modified'] = 'Modified';\n" .
				"\$DatabaseColumns['DbSession_Session']['LifeTime'] = 'LifeTime';\n" .
				"\$DatabaseColumns['DbSession_Session']['Data'] = 'Data';\n"
		);
		$Context->DatabaseTables['DbSession_Session'] = $TableName;
		$Context->DatabaseColumns['DbSession_Session']['SessionID'] = 'SessionID';
		$Context->DatabaseColumns['DbSession_Session']['Modified'] = 'Modified';
		$Context->DatabaseColumns['DbSession_Session']['LifeTime'] = 'LifeTime';
		$Context->DatabaseColumns['DbSession_Session']['Data'] = 'Data';
	}

	if (!array_key_exists('DBSESSION_DB_VERSION', $Context->Configuration)
		|| 1 > (int) $Context->Configuration['DBSESSION_DB_VERSION']
	) {

		// install DbSession_DbSession table
		$Query = 'CREATE TABLE  IF NOT EXISTS `'
			. $Context->Configuration['DATABASE_TABLE_PREFIX']
		    .'DbSession_Session` (
		  SessionID char(32),
		  Modified int,
		  LifeTime int,
		  Data text,
		  PRIMARY KEY (`SessionID`)
		);';

		$Context->Database->Execute($Query, '', 'DbSession_InstallDB', 'Could not create the Session Table');

		if ($Context->ErrorManager->Iif()) {
			AddConfigurationSetting($Context, 'DBSESSION_DB_VERSION', '1');
		}
	}
}